#include "UpdateChannelForm.h"
#include "./toObject/ModelXml.h"
#include "ProtocalInterface.h"
#include "ui_UpdateChannelForm.h"
#include <QDir>
#include <QLineEdit>
#include <QPluginLoader>
#include <QVBoxLayout>
UpdateChannelForm::UpdateChannelForm(QWidget* parent)
    : AttribBaseForm(parent)
    , ui(new Ui::UpdateChannelForm)
{
    ui->setupUi(this);
    QWidget* tcpClientWidget = new QWidget();
    QVBoxLayout* vLayout = new QVBoxLayout();
    QHBoxLayout* hLayout = new QHBoxLayout();
    {
        QLabel* label1 = new QLabel("IP地址A");
        QLineEdit* edit1 = new QLineEdit();
        m_mapChilds.insert("ipaddrA", edit1);
        QLabel* label2 = new QLabel("端口号A");
        QLineEdit* edit2 = new QLineEdit();
        m_mapChilds.insert("portA", edit2);
        hLayout->addWidget(label1);
        hLayout->addWidget(edit1);
        hLayout->addWidget(label2);
        hLayout->addWidget(edit2);
        vLayout->addLayout(hLayout);
    }
    QHBoxLayout* hLayout1 = new QHBoxLayout();
    {
        QLabel* label1 = new QLabel("IP地址B");
        QLineEdit* edit1 = new QLineEdit();
        m_mapChilds.insert("ipaddrB", edit1);
        QLabel* label2 = new QLabel("端口号B");
        QLineEdit* edit2 = new QLineEdit();
        m_mapChilds.insert("portB", edit2);
        hLayout1->addWidget(label1);
        hLayout1->addWidget(edit1);
        hLayout1->addWidget(label2);
        hLayout1->addWidget(edit2);
        vLayout->addLayout(hLayout1);
    }
    vLayout->setMargin(0);
    tcpClientWidget->setLayout(vLayout);
    ui->stackedWidget->addWidget(tcpClientWidget);
    //tcpServer配置
    QWidget* tcpServerWidget = new QWidget();
    QHBoxLayout* hLayout3 = new QHBoxLayout();
    {
        QLabel* label1 = new QLabel("服务端IP地址");
        QLineEdit* edit1 = new QLineEdit();
        m_mapChilds.insert("serverip", edit1);
        QLabel* label2 = new QLabel("服务端端口号");
        QLineEdit* edit2 = new QLineEdit();
        m_mapChilds.insert("serverport", edit2);
        hLayout3->addWidget(label1);
        hLayout3->addWidget(edit1);
        hLayout3->addWidget(label2);
        hLayout3->addWidget(edit2);
    }
    hLayout3->setMargin(0);
    tcpServerWidget->setLayout(hLayout3);
    ui->stackedWidget->addWidget(tcpServerWidget);
    //UDP配置
    QWidget* udpWidget = new QWidget();
    QVBoxLayout* vLayout6 = new QVBoxLayout();
    {
        QHBoxLayout* hLayout4 = new QHBoxLayout();
        QHBoxLayout* hLayout5 = new QHBoxLayout();
        QLabel* label1 = new QLabel("远程IP");
        QLineEdit* edit1 = new QLineEdit();
        m_mapChilds.insert("updDestIpaddr", edit1);
        QLabel* label2 = new QLabel("远程端口");
        QLineEdit* edit2 = new QLineEdit();
        m_mapChilds.insert("updDestPort", edit2);
        QLabel* label3 = new QLabel("本地IP");
        QLineEdit* edit3 = new QLineEdit();
        m_mapChilds.insert("updSrcIpaddr", edit3);
        QLabel* label4 = new QLabel("本地端口");
        QLineEdit* edit4 = new QLineEdit();
        m_mapChilds.insert("updSrcPort", edit4);

        hLayout4->addWidget(label1);
        hLayout4->addWidget(edit1);
        hLayout4->addWidget(label2);
        hLayout4->addWidget(edit2);

        hLayout5->addWidget(label3);
        hLayout5->addWidget(edit3);
        hLayout5->addWidget(label4);
        hLayout5->addWidget(edit4);

        vLayout6->addLayout(hLayout4);
        vLayout6->addLayout(hLayout5);
    }
    vLayout6->setMargin(0);
    udpWidget->setLayout(vLayout6);
    ui->stackedWidget->addWidget(udpWidget);
    //串口
    QWidget* commWidget = new QWidget();
    QHBoxLayout* hLayout7 = new QHBoxLayout();
    {
        QLabel* label1 = new QLabel("串口号");
        QLineEdit* edit1 = new QLineEdit();
        m_mapChilds.insert("comName", edit1);
        QLabel* label2 = new QLabel("波特率");
        QComboBox* combox = new QComboBox();
        combox->addItems(QStringList { "4800", "9600", "14400", "19200", "115200" });
        combox->setCurrentIndex(1);
        m_mapChilds.insert("baudRate", combox);
        QLabel* label3 = new QLabel("数据位");
        QComboBox* combox1 = new QComboBox();
        combox1->addItems(QStringList { "4", "5", "6", "7", "8" });
        combox1->setCurrentIndex(3);
        m_mapChilds.insert("dataBit", combox1);
        QLabel* label4 = new QLabel("检验位");
        QComboBox* combox2 = new QComboBox();
        combox2->addItems(QStringList { "奇", "偶", "无" });
        m_mapChilds.insert("checkBit", combox2);
        QLabel* label5 = new QLabel("停止位");
        QComboBox* combox3 = new QComboBox();
        combox3->addItems(QStringList { "1", "1.5", "2" });
        m_mapChilds.insert("stopBit", combox3);
        hLayout7->addWidget(label1);
        hLayout7->addWidget(edit1);
        hLayout7->addWidget(label2);
        hLayout7->addWidget(combox);
        hLayout7->addWidget(label3);
        hLayout7->addWidget(combox1);
        hLayout7->addWidget(label4);
        hLayout7->addWidget(combox2);
        hLayout7->addWidget(label5);
        hLayout7->addWidget(combox3);
    }
    hLayout7->setMargin(0);
    commWidget->setLayout(hLayout7);
    ui->stackedWidget->addWidget(commWidget);

    connectAllEdit();
}

UpdateChannelForm::~UpdateChannelForm()
{
    delete ui;
}

void UpdateChannelForm::InitUIData()
{
    if (ui->tabWidget->count() > 1) {
        ui->tabWidget->removeTab(1);
    }
    stUpdateChannel* updateInfo = ModelXml::GetInstance()->GetUpdateChannelInfoPointer(m_channelID);
    m_updateChannel = updateInfo;
    if (updateInfo) {
        ui->lineEdit->setText(updateInfo->name.c_str());
        ui->lineEdit_2->setText(updateInfo->desc.c_str());
        ui->lineEdit_5->setText(updateInfo->protoCate.c_str());
        ui->lineEdit_6->setText(updateInfo->protoName.c_str());

        ui->comboBox_3->setCurrentText(updateInfo->periodTime.c_str());
        ui->comboBox->setCurrentText(updateInfo->devAddr.c_str());
        ui->comboBox_2->setCurrentIndex((int)updateInfo->intarfaceType);
        ui->checkBox->setChecked(updateInfo->isDisable);
        int row = 0;
        ui->tableWidget->setRowCount(updateInfo->params.size());
        for (stPrivateParam st : updateInfo->params) {
            ui->tableWidget->setItem(row, 0, new QTableWidgetItem(QString::number(st.id)));
            ui->tableWidget->setItem(row, 1, new QTableWidgetItem(st.name.c_str()));
            ui->tableWidget->setItem(row, 2, new QTableWidgetItem(st.desc.c_str()));
            ui->tableWidget->setItem(row, 3, new QTableWidgetItem(st.value.c_str()));
            row++;
        }

        ((QLineEdit*)m_mapChilds["ipaddrA"])->setText(updateInfo->tcpClient.ipaddrA.c_str());
        ((QLineEdit*)m_mapChilds["ipaddrB"])->setText(updateInfo->tcpClient.ipaddrB.c_str());
        ((QLineEdit*)m_mapChilds["portA"])->setText(updateInfo->tcpClient.portA.c_str());
        ((QLineEdit*)m_mapChilds["portB"])->setText(updateInfo->tcpClient.portB.c_str());

        ((QLineEdit*)m_mapChilds["serverip"])->setText(updateInfo->tcpServer.ipaddr.c_str());
        ((QLineEdit*)m_mapChilds["serverport"])->setText(updateInfo->tcpServer.port.c_str());

        ((QLineEdit*)m_mapChilds["updDestIpaddr"])->setText(updateInfo->udp.remoteIpaddr.c_str());
        ((QLineEdit*)m_mapChilds["updDestPort"])->setText(updateInfo->udp.remotePort.c_str());
        ((QLineEdit*)m_mapChilds["updSrcIpaddr"])->setText(updateInfo->udp.remoteIpaddr.c_str());
        ((QLineEdit*)m_mapChilds["updSrcPort"])->setText(updateInfo->udp.remotePort.c_str());

        ((QLineEdit*)m_mapChilds["comName"])->setText(updateInfo->serialport.name.c_str());
        ((QComboBox*)m_mapChilds["baudRate"])->setCurrentText(updateInfo->serialport.baudRate.c_str());
        ((QComboBox*)m_mapChilds["dataBit"])->setCurrentText(updateInfo->serialport.dataBit.c_str());
        ((QComboBox*)m_mapChilds["checkBit"])->setCurrentText(updateInfo->serialport.checkBit.c_str());
        ((QComboBox*)m_mapChilds["stopBit"])->setCurrentText(updateInfo->serialport.stopBit.c_str());
        QWidget* widget = GetProtocalConfigWidget();
        if (widget) {
            ui->tabWidget->addTab(widget, "设备点表");
            ui->tabWidget->setIconSize(QSize(20, 20));
            ui->tabWidget->setTabIcon(1, QIcon(":/resource/pointTable.png"));
            ui->tabWidget->setCurrentIndex(1);
        }
    }
}

void UpdateChannelForm::saveModel()
{
    saveChanngeData();
}

void UpdateChannelForm::saveChanngeData()
{
    stUpdateChannel* updateInfo = ModelXml::GetInstance()->GetUpdateChannelInfoPointer(m_channelID);
    if (!updateInfo)
        return;

    updateInfo->name = ui->lineEdit->text().toStdString();
    updateInfo->desc = ui->lineEdit_2->text().toStdString();
    updateInfo->protoCate = ui->lineEdit_5->text().toStdString();
    updateInfo->protoName = ui->lineEdit_6->text().toStdString();

    updateInfo->periodTime = ui->comboBox_3->currentText().toStdString();
    updateInfo->devAddr = ui->comboBox->currentText().toStdString();
    updateInfo->isDisable = ui->checkBox->checkState() ? 1 : 0;
    updateInfo->intarfaceType = (stUpdateChannel::Intarface_Type)ui->comboBox_2->currentIndex();

    updateInfo->tcpClient.ipaddrA = ((QLineEdit*)m_mapChilds["ipaddrA"])->text().toStdString();
    updateInfo->tcpClient.portA = ((QLineEdit*)m_mapChilds["portA"])->text().toStdString();
    updateInfo->tcpClient.ipaddrB = ((QLineEdit*)m_mapChilds["ipaddrB"])->text().toStdString();
    updateInfo->tcpClient.portB = ((QLineEdit*)m_mapChilds["portB"])->text().toStdString();

    updateInfo->tcpServer.ipaddr = ((QLineEdit*)m_mapChilds["serverip"])->text().toStdString();
    updateInfo->tcpServer.port = ((QLineEdit*)m_mapChilds["serverport"])->text().toStdString();

    updateInfo->udp.localIpaddr = ((QLineEdit*)m_mapChilds["updSrcIpaddr"])->text().toStdString();
    updateInfo->udp.localPort = ((QLineEdit*)m_mapChilds["updSrcPort"])->text().toStdString();
    updateInfo->udp.remoteIpaddr = ((QLineEdit*)m_mapChilds["updDestIpaddr"])->text().toStdString();
    updateInfo->udp.remotePort = ((QLineEdit*)m_mapChilds["updDestPort"])->text().toStdString();

    updateInfo->serialport.name = ((QLineEdit*)m_mapChilds["comName"])->text().toStdString();
    updateInfo->serialport.baudRate = ((QComboBox*)m_mapChilds["baudRate"])->currentText().toStdString();
    updateInfo->serialport.dataBit = ((QComboBox*)m_mapChilds["dataBit"])->currentText().toStdString();
    updateInfo->serialport.stopBit = ((QComboBox*)m_mapChilds["stopBit"])->currentText().toStdString();
    updateInfo->serialport.checkBit = ((QComboBox*)m_mapChilds["checkBit"])->currentText().toStdString();

    updateInfo->params.clear();
    for (int i = 0; i < ui->tableWidget->rowCount(); ++i) {
        stPrivateParam param;
        param.id = ui->tableWidget->item(i, 0)->text().toInt();
        param.name = ui->tableWidget->item(i, 1)->text().toStdString();
        param.desc = ui->tableWidget->item(i, 2)->text().toStdString();
        param.value = ui->tableWidget->item(i, 3)->text().toStdString();
        updateInfo->params.push_back(param);
    }
}

QWidget* UpdateChannelForm::GetProtocalConfigWidget()
{
    QPluginLoader loader;
    QString pluginPath = QDir::currentPath() + QString("/Update/") + m_updateChannel->protoName.c_str() + "/" + m_updateChannel->protoName.c_str() + "UI.dll";
    loader.setFileName(pluginPath);
    if (!loader.load()) {
        return nullptr;
    }
    QObject* plugin = loader.instance();
    qDebug() << __FUNCTION__ << loader.errorString();
    if (plugin == NULL) {
        qDebug("loader.instance() is null");
        return nullptr;
    }
    UpdateProtocalInterface* pi = qobject_cast<UpdateProtocalInterface*>(plugin);
    if (pi) {
        if (m_pointWidget) {
            delete m_pointWidget;
            m_pointWidget = nullptr;
        }
        m_pointWidget = pi->getWidget();
        if (m_updateChannel->pointTable_node.empty())
            m_updateChannel->createXML();
        std::map<int, stCollectChannel>* p1 = ModelXml::GetInstance()->GetCollectChannelPointer();
        pi->SetCollectData(ModelXml::GetInstance()->GetCollectChannelPointer(), ModelXml::GetInstance()->GetAllDevice());
        pi->InitData(m_updateChannel->pointTable_node);

        std::string str = m_updateChannel->pointTable_node.name();
        str = m_updateChannel->pointTable_node.child("tag").attribute("name").value();
        qDebug("Success2");
        if (m_pointWidget) {
            return m_pointWidget;
        }
    }
    return nullptr;
}

void UpdateChannelForm::on_comboBox_2_currentIndexChanged(int index)
{
    ui->stackedWidget->setCurrentIndex(index);
}
